<template>
  <div>
    <!-- 添加按钮 -->
    <el-button type="primary" icon="el-icon-plus" @click="addTrademark"
      >添加</el-button
    >

    <!-- 表格 -->
    <el-table :data="trademarkList" border style="width: 100%; margin: 20px 0">
      <el-table-column
        align="center"
        type="index"
        prop="prop"
        label="序号"
        width="80"
      >
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="width">
      </el-table-column>
      <el-table-column prop="prop" label="品牌logo" width="width">
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" alt="" width="100px" height="80px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-edit"
            @click="updateTrademark(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      style="text-align: center"
      background
      @current-change="getTrademarkList"
      :current-page="page"
      :page-size="limit"
      :total="total"
      :pager-count="7"
      :page-sizes="[3, 5, 10]"
      @size-change="handleSizeChange"
      layout="prev, pager, next,jumper,->,sizes,total"
    >
    </el-pagination>

    <!-- 添加品牌管理的对话框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form style="width: 80%" v-model="tmForm">
        <el-form-item label="品牌名称" label-width="100px">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌logo" label-width="100px">
          <!-- 上传图片组件 -->
          <el-upload
            class="avatar-uploader"
            action="http://39.98.123.211/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">
              只能上传jpg/png文件，且不超过2M
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTrademark"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Trademark",
  data() {
    return {
      //当前页码
      page: 1,
      //每页记录数
      limit: 3,
      //总条数
      total: 0,
      //品牌列表
      trademarkList: [],

      // 控制对话框隐藏与显示
      dialogFormVisible: false,

      //品牌收集的表单信息
      tmForm: {
        id: "",
        //品牌名称
        tmName: "",
        //上传图片imgurl
        logoUrl: "",
      },
    };
  },
  methods: {
    //获取品牌列表 或 点击分页切换
    async getTrademarkList(page = 1) {
      this.page = page;
      try {
        const result = await this.$API.trademark.getTrademarkList(
          this.page,
          this.limit
        );
        if (result.code === 200) {
          this.trademarkList = result.data.records;
          this.total = result.data.total;
        }
      } catch (error) {
        this.$message.error("请求相应失败:" + error.messag);
      }
    },

    //切换分页条数
    handleSizeChange(limit) {
      this.limit = limit;
      this.getTrademarkList();
    },

    //上传图片之前检查函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    //添加成功获取图片url
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },

    //添加trademark
    addTrademark() {
      this.tmForm = {
        tmName: "",
        logoUrl: "",
      };
      //显示dialog
      this.dialogFormVisible = true;
    },

    //修改trademark
    updateTrademark(row) {
      //解构赋值浅拷贝(重要!)
      this.tmForm = { ...row };
      this.dialogFormVisible = true;
    },

    //请求添加或修改trademark
    async addOrUpdateTrademark() {
      this.dialogFormVisible = false;
      try {
        await this.$API.trademark.addTrademarkOrUpdate(
          this.tmForm
        );
        this.$message.success('更新成功!')
        //重新获取数据
        this.getTrademarkList(this.tmForm.id?this.page:1);
      } catch (error) {
        this.$message.error('添加失败:'+error.message)
      }
    },
  },

  mounted() {
    //获取品牌分页列表
    this.getTrademarkList();
  },
};
</script>

<style>
/* 以下为文件上传样式 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style scoped>
</style>
